import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../generated/assets.dart';

class PrivacyWidget extends StatefulWidget {
  final con =
      WebViewController()..setJavaScriptMode(JavaScriptMode.unrestricted);
  // ..loadFile("${Assets.htmlIndex}#/policy");
  // ..loadRequest(
  //   Uri.parse(
  //     "http://119.29.142.236:8082/static/dist/index.html#/policy",
  //   ),
  // );
  PrivacyWidget({super.key});

  @override
  State<PrivacyWidget> createState() => _PrivacyWidgetState();
}

class _PrivacyWidgetState extends State<PrivacyWidget> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    widget.con
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress:
              (progress) =>
                  print('=============== Loading progress: $progress%'),
          onHttpError: (error) => {
            print('=============== error : $error'),
          },
          onPageStarted: (url) => print('=============== Page started: $url'),

          onPageFinished: (url) async {
            await widget.con.runJavaScript(
              "window.location.hash = '#/policy';",
            );
          },
          onNavigationRequest: (request) {
            print('=============== Navigating to: ${request.url}');
            return NavigationDecision.navigate;
          },
        ),
      )
      // ..loadRequest(Uri.parse('https://www.baidu.com/'));
      // ..loadHtmlString("${Assets.htmlIndex}#/policy");
      ..loadFlutterAsset(Assets.htmlIndex);
      // ..loadFile(Assets.htmlIndex);
  }

  @override
  void dispose() {
    widget.con.clearCache(); // 清理缓存
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('飞机3')),
      body: WebViewWidget(controller: widget.con),
    );
  }
}
